#include "Avalon/IOTests/MockConnection.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::IO::Tests;
using namespace boost;

MockConnection::MockConnection()
    : m_isOpen(false) {}

MockConnection::~MockConnection() {
  Close();
}

void MockConnection::SetOpen() {
  lock_guard<mutex> lock(m_mutex);
  m_isOpen = true;
}

bool MockConnection::IsOpen() const {
  lock_guard<mutex> lock(m_mutex);
  return m_isOpen;
}

void MockConnection::Close() {
  shared_ptr<ClosedSlot> closedSlot;
  {
    lock_guard<mutex> lock(m_mutex);
    if(!m_isOpen) {
      return;
    }
    m_isOpen = false;
    closedSlot = m_closedSlot;
  }
  (*closedSlot)();
}

void MockConnection::SetClosedSlot(const ClosedSlot& slot, Out<bool> isOpen) {
  lock_guard<mutex> lock(m_mutex);
  *isOpen = m_isOpen;
  m_closedSlot.reset(new ClosedSlot(slot));
}
